﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VaccineEbook.Common;

namespace VaccineEbook.Models.DBEntity
{
    public partial class User
    {
        internal static User LoginOnWeb(string _username, string _password, ref string _messageError)
        {
            VaccineEBookEntities context = null;
            string encodePassword = EncryptionUtil.Encode(_password, _username.Trim().ToLower());
            try
            {
                context = new VaccineEBookEntities();

                var query = from u in context.Users
                            where u.UserStatus == (int)USER_STATUS.WEB_ACTIVE &&
                            u.Username == _username &&
                            u.Password == encodePassword
                            select u;
                if (query.Count() > 0)
                {
                    return query.First();
                }
                else
                {
                    _messageError = "Does not exist";
                    return null;
                }
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }

        internal static IEnumerable<User> GetAll(ref string _messageError)
        {
            VaccineEBookEntities context = null;
            try
            {
                context = new VaccineEBookEntities(Constant.DEFAUT_TIMEOUT_SECOND);
                return context.Users.OrderBy(tb => tb.Username);
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }

        public enum USER_STATUS
        {
            WAITTING,
            MOBILE_ACTIVE,
            WEB_ACTIVE,
            WEB_MOBILE_ACTIVE,
            DEACTIVATED
        }

        internal static User GetBy_Username(string _username, ref string _messageError)
        {
            VaccineEBookEntities context = null;
            try
            {
                context = new VaccineEBookEntities();
                var query = from u in context.Users
                            where u.Username == _username
                            select u;
                return query.First();
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }

        internal static IEnumerable<Token> GetAllToken(ref string _messageError)
        {
            VaccineEBookEntities context = null;
            try
            {
                context = new VaccineEBookEntities();
                var query = from tk in context.Tokens                            
                            select tk;
                return  query.ToList().AsQueryable();
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return null;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }

      
        internal static bool ResetPassword(string  _username, string _oldPassword, string newPassword, ref string _messageError)
        {
            VaccineEBookEntities context = null;
            string encodePassword = EncryptionUtil.Encode(_oldPassword, _username.Trim().ToLower());
            try
            {
                context = new VaccineEBookEntities();

                var query = from u in context.Users
                            where u.UserStatus == (int)USER_STATUS.WEB_ACTIVE &&
                            u.Username == _username &&
                            u.Password == encodePassword
                            select u;
                if (query.Count() > 0)
                {
                    User user = query.First();
                    user.Password = EncryptionUtil.Encode(newPassword, _username.Trim().ToLower());
                    context.SaveChanges();
                    return true;
                }
                else
                {
                    _messageError = "Old password does not match!";
                    return false ;
                }
            }
            catch (Exception ex)
            {
                _messageError = ex.Message;
                return false;
            }
            finally
            {
                context = null;
                GC.Collect();
            }
        }
    }
}